package org.apache.hadoop.mapreduce;

import java.util.LinkedList;

public class IterativeJobSet implements Runnable {
	private IterativeJob firstJob;
	private IterativeJob lastJob;
	private LinkedList<Job> joblist;
	private ITerminateCondition tcondition;
	private long iteration;

	public IterativeJobSet() {
		this.joblist = new LinkedList<Job>();
		iteration = 0;
	}

	public void addJob(IterativeJob job) {
		synchronized (this) {
			if (this.firstJob == null) {
				this.firstJob = job;
			}
			this.lastJob = job;
			job.setIterative(true);
			joblist.add(job);
		}
	}

	public Job getFirstJob() {
		return firstJob;
	}

	public long getIteration() {
		return iteration;
	}

	public LinkedList<Job> getJoblist() {
		return joblist;
	}

	public Job getLastJob() {
		return lastJob;
	}

	public ITerminateCondition getTcondition() {
		return tcondition;
	}

	@Override
	public void run() {
		try {
			do {
				for (Job job : joblist) {
					job.submit();
				}
				iteration++;
			} while (!tcondition.terminate());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public void setFirstJob(IterativeJob firstJob) {
		this.firstJob = firstJob;
	}

	public void setIteration(long iteration) {
		this.iteration = iteration;
	}

	public void setJoblist(LinkedList<Job> joblist) {
		this.joblist = joblist;
	}

	public void setLastJob(IterativeJob lastJob) {
		this.lastJob = lastJob;
	}

	public void setTcondition(ITerminateCondition tcondition) {
		this.tcondition = tcondition;
	}
}
